package com.mz.ozy.activity.me;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.mz.ozy.R;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.FriendBean;
import com.mz.ozy.bean.PronotionBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.FriendPresenter;
import com.mz.ozy.mvp.view.FriendContract;
import com.mz.ozy.utils.QiNiuManger;
import com.mz.ozy.utils.SPUtils;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.utils.Utils;
import com.mz.ozy.utils.glideUtils.GlideUtils;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.UpCompletionHandler;
import com.tbruyelle.rxpermissions.RxPermissions;

import org.json.JSONObject;

import java.util.List;

import androidx.annotation.Nullable;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.functions.Action1;

/**
 * @author lifeStyle on 2020/4/1 0001.
 */
public class UpdataActivity extends BaseActivity<FriendContract.View, FriendPresenter> implements FriendContract.View {
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_submit_certify)
    TextView tvSubmitCertify;
    @BindView(R.id.tv_invite_title)
    TextView tvInviteTitle;
    @BindView(R.id.tv_num)
    TextView tvNum;

    @BindView(R.id.ll_visi)
    LinearLayout llVisi;
    @BindView(R.id.btn_comit)
    Button btnComit;
    @BindView(R.id.iamge_show)
    ImageView iamgeShow;
    private String url;
    private int type;
    private String id;
    private boolean submit = false;
    private String qiNiuKey;
    private PronotionBean pronotionBean;

    public static void go(Context context, String id, int type, boolean submit) {
        Intent intent = new Intent(context, UpdataActivity.class);
        intent.putExtra("id", id);
        intent.putExtra("type", type);
        intent.putExtra("submit", submit);
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_updata);
        ButterKnife.bind(this);
        initTitle();
        type = getIntent().getIntExtra("type", 0);
        id = getIntent().getStringExtra("id");
        submit = getIntent().getBooleanExtra("submit", false);
        if (type == 0) {
            tvCenterTitle.setText("会员晋升");
            tvInviteTitle.setText("我邀请的健康顾问");
        } else {
            tvCenterTitle.setText("升级服务网点");
            tvInviteTitle.setText("我邀请的社区服务网点");
        }
        mPresenter.vipUpDataSh(this, id, type);
    }

    @Override
    public void getFriendSuccess(FriendBean bean) {

    }

    @Override
    public void getVipUpSuccess(JSONObject bean) {
        finish();
        ToastUtils.showToast("提交成功");
        // startActivity(new Intent(UpdataActivity.this, PromotionTypeActivity.class));
    }

    @Override
    public void getVipUpShSuccess(FriendBean bean) {

    }

    @Override
    public void getVipUpDataInfoSuccess(PronotionBean bean) {
        tvNum.setText(bean.getGwnum() + "/" + bean.getZtnum());
        pronotionBean = bean;
        if (bean.getIfup() == 0) {
            llVisi.setVisibility(View.GONE);
            btnComit.setVisibility(View.GONE);
            Drawable drawable = getResources().getDrawable(R.mipmap.asset_kt_icon);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            tvType.setCompoundDrawables(null, null, null, null);
            tvType.setText("未达到升级条件，继续加油！");
        } else {
            String msg = "";
            if (type == 0) {
                switch (bean.getGrade()) {
                    case 0:
                        msg = "普通会员";
                        break;
                    case 1:
                        msg = "VIP";
                        break;
                    case 2:
                        msg = "金卡VIP";
                        break;
                    case 3:
                        msg = "钻卡VIP";
                        break;
                    case 4:
                        msg = "健康顾问";
                        break;
                    case 5:
                        msg = "资深顾问";
                        break;
                    case 6:
                        msg = "蓝宝顾问";
                        break;
                    case 7:
                        msg = "绿宝顾问";
                        break;
                    case 8:
                        msg = "红宝督导";
                        break;
                    case 9:
                        msg = "蓝钻督导";
                        break;
                    case 10:
                        msg = "绿钻督导";
                        break;
                    case 11:
                        msg = "红钻督导";
                        break;
                    case 12:
                        msg = "黑钻首席";
                        break;
                }
            } else {
                switch (bean.getGrade()) {
                    case 1:
                        msg = "社区服务网点";
                        break;
                    case 2:
                        msg = "区级服务网点";
                        break;
                    case 3:
                        msg = "市级服务网点";
                        break;
                    case 4:
                        msg = "地区级服务网点";
                        break;
                }
            }
            tvType.setText("您可升级" + msg);
            if (type == 0) {
                tvSubmitCertify.setText("请提交您的" + msg + "结业证书");
            } else {
                tvSubmitCertify.setText("请提交您的升级证书");
            }
        }

    }

    @OnClick({R.id.iamge_show, R.id.btn_comit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iamge_show:
                RxPermissions.getInstance(UpdataActivity.this)
                        .request(Manifest.permission.CAMERA)//这里填写所需要的权限
                        .subscribe(new Action1<Boolean>() {
                            @Override
                            public void call(Boolean aBoolean) {
                                if (aBoolean) {//true表示获取权限成功（注意这里在android6.0以下默认为true）
                                    PictureSelector.create(UpdataActivity.this)
                                            .openGallery(PictureMimeType.ofImage())
                                            .imageSpanCount(4)
                                            .selectionMode(PictureConfig.SINGLE)
                                            .forResult(PictureConfig.CHOOSE_REQUEST);
                                } else {
                                    Log.i("permissions", Manifest.permission.READ_EXTERNAL_STORAGE + "：" + "获取失败");
                                }
                            }
                        });

                break;
            case R.id.btn_comit:
                if (TextUtils.isEmpty(url)) {
                    toastUploadFail();
                    return;
                }
                uploadAvatar();
                break;
        }
    }

    private void toastUploadFail() {
        if (type == 0) {
            String msg = "";
            switch (pronotionBean.getGrade()) {
                case 0:
                    msg = "普通会员";
                    break;
                case 1:
                    msg = "VIP";
                    break;
                case 2:
                    msg = "金卡VIP";
                    break;
                case 3:
                    msg = "钻卡VIP";
                    break;
                case 4:
                    msg = "健康顾问";
                    break;
                case 5:
                    msg = "资深顾问";
                    break;
                case 6:
                    msg = "蓝宝顾问";
                    break;
                case 7:
                    msg = "绿宝顾问";
                    break;
                case 8:
                    msg = "红宝督导";
                    break;
                case 9:
                    msg = "蓝钻督导";
                    break;
                case 10:
                    msg = "绿钻督导";
                    break;
                case 11:
                    msg = "红钻督导";
                    break;
                case 12:
                    msg = "黑钻首席";
                    break;
            }
            ToastUtils.showToast("您的" + msg + "结业证书上传不成功");
        } else {
            ToastUtils.showToast("请提交您的升级证书");
        }
    }

    private void uploadAvatar() {
        if (TextUtils.isEmpty(qiNiuKey)) {
            toastUploadFail();
            return;
        }
        if (mPresenter != null) {
            if (submit) {
                mPresenter.vipUp(UpdataActivity.this, qiNiuKey, "", type);
            } else {
                mPresenter.vipUp(UpdataActivity.this, qiNiuKey, id, type);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PictureConfig.CHOOSE_REQUEST && resultCode == RESULT_OK) {
            List<LocalMedia> images = PictureSelector.obtainMultipleResult(data);
            List<String> uploadlist = Utils.convertToImageList(images);
            if (uploadlist != null && uploadlist.size() > 0) {
                url = uploadlist.get(0);

                QiNiuManger.getInstance().getUploadManager().put(url, QiNiuManger.getInstance().getKeyName(), SPUtils.getInstance(this).getString(Constants.SPName.QINIU_TOKEN, ""), new UpCompletionHandler() {
                    @Override
                    public void complete(String key, ResponseInfo info, JSONObject response) {
                        if (info.isOK()) {
                            qiNiuKey = key;
                            GlideUtils.loadImg(UpdataActivity.this, iamgeShow, url);
                        }
                    }
                }, null);
            }
        }
    }
}



